<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="toGoodsAdd()" :icon="CirclePlus">添加商品</el-button>
    </div>

    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">商品名称：</span>
        <el-input placeholder="请输入名称" v-model="searchParams.name" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
<!--      <div class="task-one__item">-->
<!--        <span class="task-one__label">商品库存：</span>-->
<!--        <el-input placeholder="请输入数字" v-model.number="searchParams.inventory" @keyup.enter.native="handleSearch"-->
<!--                  clearable></el-input>-->
<!--      </div>-->
      <div class="task-one__btn ">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
<!--        <el-button type="success" @click="importData('admin/order/rentOrder/export',searchParams)" :icon="Download">导出</el-button>-->
      </div>
    </div>

    <el-tabs v-model="tabsActive" type="card" >
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <template v-for="(item, index) in selectOptions.productStatus">
        <el-tab-pane :label="item.label" :name="item.value"></el-tab-pane>
      </template>
    </el-tabs>

<!--    <div style="margin-bottom: 10px">-->
<!--      <span style="margin-right: 20px">已选：0</span>-->
<!--      <el-button plain size="small"  @click="">批量删除</el-button>-->
<!--      <el-button plain size="small"  @click="">批量上架</el-button>-->
<!--      <el-button plain size="small"  @click="">批量下架</el-button>-->
<!--    </div>-->

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)" @selection-change="handleAllSelected">
<!--      <el-table-column type="selection" width="55" />-->
      <el-table-column prop="mainImage" label="主图" min-width="80" width="80">
        <template #default="{ row }">
          <el-image style="width: 65px; height: 65px; border: 1px solid rgb(204, 204, 204); padding: 3px; border-radius: 5%;" :src="getImgs(row.mainImage.imagePath)" fit="cover" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品" min-width="260">
        <template #default="{ row }">
          <p>{{row.name}}</p>
          <p><span style="font-weight: 700;">￥{{row.price}}</span></p>
          <p>
            <span v-if="row.classification" style="margin-right: 20px">
              <el-text type="info">分类：</el-text>{{row.classification.name}}
            </span>
            <span style="margin-right: 20px">
              <el-text type="info">规格：</el-text>{{row.specifications}}
            </span>
            <span style="margin-right: 20px">
              <el-text type="info">库存：</el-text>{{row.inventory}}件
            </span>
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="productStatus" label="状态"  width="100">
        <template #default="{ row }">
          <el-text v-if="row.productStatus === 3" type="warning">已下架</el-text>
          <el-text v-else-if="row.productStatus === 1" type="success">已上架</el-text>
          <el-text v-else-if="row.productStatus === 4" type="danger">已售罄</el-text>
          <el-text v-else-if="row.productStatus == 2" type="primary">待上架</el-text>
          <el-text v-else type="info">-</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="salesVolume" label="总销量"  width="100">
        <template #default="{ row }">
          <span>{{row.salesVolume}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="created" label="创建时间" width="160"></el-table-column>
      <el-table-column prop="updated" label="更新时间" width="160"></el-table-column>
      <el-table-column label="操作" min-width="300" width="300" fixed="right">
        <template #default="{ row }">
          <el-button class="action-btn" size="small" plain type="primary" @click="toGoodsEdit(row)">编辑</el-button>
          <el-button class="action-btn" size="small" plain type="warning" @click="handleAction('put',`product/update-status/${row.sid}`)" v-if="row.productStatus === 1">下架</el-button>
          <el-button class="action-btn" size="small" plain type="warning" @click="handleAction('put',`product/update-status/${row.sid}`)" v-if="row.productStatus === 2 || row.productStatus === 3">上架</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row.sid)">
            <template #reference>
              <el-button size="small" plain type="danger" class="action-btn">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        v-model:current-page="page"
        v-model:page-size="pageSize"
        :page-sizes="pageSizeNum"
        :total="totalNums"
        layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch,
  computed
} from "vue"
import listMixins from "@mixins/hook/list"
import {
  useRouter
} from "vue-router"
import {
  CirclePlus,Search,Download
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"

const router = useRouter()
const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction,
  handleDelete
} = listMixins()

const props = defineProps({
  type: {
    type: Number,
    default: 1,
  },
})
apiPath.value = 'product';
searchParams.type = props.type

onMounted(() => {
  getData()
})
const tabsActive = ref('all')
searchParams.productStatus = computed(()=>{
  if(tabsActive.value == 'all'){
    return ''
  }
  return tabsActive.value
})
watch(tabsActive,()=>{
  page.value = 1
  getData()
})
const getImgs = (src) =>{
  return proxy.imgUrl + src
}
const selectOptions = reactive({
  productStatus:[
    {label:'已上架',value:1},
    {label:'待上架',value:2},
    {label:'已下架',value:3},
    {label:'已售罄',value:4}
  ],
})
const toGoodsEdit = (row) =>{
  if(props.type === 0){
    router.push({
      path: "/Goods/ProductEdit",
      query: {
        sid:row.sid
      }
    })
  }
  if(props.type === 1){
    router.push({
      path: "/Goods/GoodsEdit",
      query: {
        sid:row.sid
      }
    })
  }

}
const toGoodsAdd = () =>{
  if(props.type === 0){
    router.push({
      path: "/Goods/ProductAdd",
      query: {

      }
    })
  }
  if(props.type === 1){
    router.push({
      path: "/Goods/GoodsAdd",
      query: {

      }
    })
  }
}
</script>

<style lang="less">


</style>